<?php
include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Stage.php";

/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/21
 * Time: 9:54
 */
class StageDAO{
    const FIELD_ID = "stageId";
    const FIELD_TASK_ID = "taskId";
    const FIELD_NAME = "stageName";
    const FIELD_DESCRIPTION = "stageDesc";
    const FIELD_DEADLINE = "deadline";
    const FIELD_REWARD = "reward";
    const FIELD_INDEX = "stageIndex";
    const FIELD_WORKER_NUM = "workerNum";
    const FIELD_AGGREGATE_METHOD = "aggregateMethod";
    const FIELD_AGGREGATE_RESULT = "aggregateResult";
    const FIELD_RESTRICTIONS = "restrictions";
    const FIELD_DURATION = "stageDuration";
    const FIELD_CONTRACT = "stageContract";
    const FIELD_STATUS = "stageStatus";

    public $conn;

    function __construct()
    {
        $this->conn = new db();
    }

    //插入阶段
    public function insert($stage){

    }

    //删除阶段
    public function delete($stageId){

    }

    //查询阶段
    public function query($stageId){
        $sql = "select * from stage where ".self::FIELD_ID." = ".$stageId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $stage = $this->loadDataFromResult($row);
            return $stage;
        }
        else
            return null;
    }

    //更新阶段
    public function update($stage){
        if($this->query($stage->getId()) == null)
            return -1;
        //更新操作
        $db = $this->conn->getDBConn();
        $stmt = $db->prepare("update stage set ".self::FIELD_NAME." = ?, ".self::FIELD_DESCRIPTION." = ?, ".self::FIELD_DEADLINE." = ?, "
            .self::FIELD_REWARD." = ?, ".self::FIELD_INDEX." = ?, ".self::FIELD_WORKER_NUM." = ?, ".self::FIELD_AGGREGATE_METHOD." = ?, ".self::FIELD_AGGREGATE_RESULT." = ?, "
            .self::FIELD_RESTRICTIONS." = ?, ".self::FIELD_DURATION." = ?, ".self::FIELD_CONTRACT." = ?, ".self::FIELD_STATUS." = ? where ".self::FIELD_ID." = ?");
        $stmt->bindParam(1,$stage->getName());
        $stmt->bindParam(2,$stage->getDescription());
        $stmt->bindParam(3,$stage->getDeadline());
        $stmt->bindParam(4,$stage->getReward());
        $stmt->bindParam(5,$stage->getIndex());
        $stmt->bindParam(6,$stage->getWorkerNum());
        $stmt->bindParam(7,$stage->getAggregateMethod());
        $stmt->bindParam(8,$stage->getAggregateResult());
        $stmt->bindParam(9,$stage->getRestrictions());
        $stmt->bindParam(10,$stage->getDuration());
        $stmt->bindParam(11,$stage->getContract());
        $stmt->bindParam(12,$stage->getStatus());
        $stmt->bindParam(13,$stage->getId());
        $result = $stmt->execute();
        return $result;
    }

    //根据taskid查找所有stage
    public function queryStageListByTask($taskId){
        $sql = "select * from stage where ".self::FIELD_TASK_ID." = ".$taskId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $stages = array();
            foreach ($rows as $row){
                $stage = $this->loadDataFromResult($row);
                array_push($stages,$stage);
            }
            return $stages;
        }
        else
            return null;
    }

    //根据task和所处stage下标查询stage
    public function queryStageByTaskAndIndex($taskId,$index){
        $sql = "select * from stage where ".self::FIELD_TASK_ID." = ".$taskId." and ".self::FIELD_INDEX." = ".$index;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $stage = $this->loadDataFromResult($row);
            return $stage;
        }
        else
            return null;
    }

    //从结果集中构建出task对象
    private function loadDataFromResult($row){
        $stage = new Stage();
        $stage->setId($row[self::FIELD_ID]);
        $stage->setDescription($row[self::FIELD_DESCRIPTION]);
        $stage->setTaskId($row[self::FIELD_TASK_ID]);
        $stage->setName($row[self::FIELD_NAME]);
        $stage->setDeadline($row[self::FIELD_DEADLINE]);
        $stage->setReward($row[self::FIELD_REWARD]);
        $stage->setIndex($row[self::FIELD_INDEX]);
        $stage->setWorkerNum($row[self::FIELD_WORKER_NUM]);
        $stage->setAggregateMethod($row[self::FIELD_AGGREGATE_METHOD]);
        $stage->setAggregateResult($row[self::FIELD_AGGREGATE_RESULT]);
        $stage->setRestrictions($row[self::FIELD_RESTRICTIONS]);
        $stage->setDuration($row[self::FIELD_DURATION]);
        $stage->setContract($row[self::FIELD_CONTRACT]);
        $stage->setStatus($row[self::FIELD_STATUS]);
        return $stage;
    }

}
